/**
 	PagingNavigator class
 */
package nrb.utils
{
	import flash.display.Sprite;
	
	import mx.collections.ArrayCollection;
	import mx.events.ItemClickEvent;
	import mx.controls.Alert;
	
	/**
	 	Type: Class
	 	@Params:none
	 	@return:none
	 	Desc: Class for paging using togglebuttonbar
	 */
    public class PagingNavigator extends Sprite {
		
		/**
		 	instantiate variables
		 */
		[Bindable] public var myData:ArrayCollection = new ArrayCollection();
		[Bindable] public var nav:ArrayCollection = new ArrayCollection();
		public var page_size:uint = 10;
		public var navSize:uint = 10;
		private var index:uint = 0;
		private var navPage:uint = 1;
		private var total_records:Number;
		private var listener:Function;

		
		/**
		 	Type: Controller
		 	@Params:none
		 	@return:none
		 	Desc: Initializes variables 
		 */
        public function PagingNavigator(obj:Object) {
           total_records = obj.totalrecords;
           page_size = obj.pagesize;
           listener = new Function();
           listener = obj.listener;
           createNavBar(Math.ceil(total_records/page_size));
        }
        
		
		private function createNavBar(pages:uint = 1,set:uint = 0): void {
			nav.removeAll();
			if( pages > 1 )
			{
				navSize = 10;
				if( set != 0 )
				{
					nav.addItem({label:"<<",data:0});
					if( (set - navSize ) >= 0 )
					{
						nav.addItem({label:"<",data:set - navSize});
					}
					else
					{
						nav.addItem({label:"<",data:0});
					}
				}
				
				//check if pages is more than navigator size
				//if(pages < navSize){
				//	navSize = pages;
				//}
				
				if((pages - set) < navSize)
					navSize = (pages - set);
				
				
				for( var x:uint = 0; x < navSize; x++)
				{
					var pg:uint = x + set;
					nav.addItem({label: pg + 1,data: pg});
				}
				
				if( pg < pages - 1 )
				{
					nav.addItem({label:">",data:pg + 1});
					//nav.addItem({label:">>",data:pages - page_size});
					nav.addItem({label:">>",data:(Math.floor(pages/page_size)*page_size)});
				}
			}
		}
		
		public function navigatePage(event:ItemClickEvent, pageNav:Object): void {
			//refreshDataProvider(event.item.data);
			listener(event.item.data);
			var lb:String = event.item.label.toString();
			if( lb.indexOf("<") > -1 || lb.indexOf(">") > -1 )
			{
				createNavBar(Math.ceil(total_records/page_size),event.item.data);
				if( event.item.data == 0 )
				{
					pageNav.selectedIndex = 0;
				}
				else
				{
					pageNav.selectedIndex = 2;
				}
			}
			
		}
		
		public function has_children(): Boolean {
			if(nav.length > 0){
				return true;
			}else{
				return false;
			}
		}
		
		/*
		private function refreshDataProvider(start:uint): void {
			//myData = new ArrayCollection( orgData.source.slice((start * pageSize),(start * pageSize) + pageSize) );
			//pass here to listener the index and limit
		}
		*/

       

		
		
	}
}